﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class KhachHangDAO
    {
        SqlConnection conn = null;

        public List<KhachHangDTO> LayDanhSachKhachHang()
        {
            List<KhachHangDTO> lisKH = new List<KhachHangDTO>();
            SqlDataReader reader = null;
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_LayDanhSachKhachHang";
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    KhachHangDTO khDTO = new KhachHangDTO();
                    khDTO.MaKhachHang = reader.GetInt32(0);
                    khDTO.TenKhachHang = reader.GetString(1);
                    khDTO.SoDienThoai = reader.GetString(2);
                    khDTO.DiaChi = reader.GetString(3);
                    khDTO.Email = reader.GetString(4);
                    khDTO.GhiChu = reader.GetString(5);

                    lisKH.Add(khDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return lisKH;
        }

        public KhachHangDTO LayThongTinKhachHangTheoMa(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_LayThongTinKhachHangTheoMa";
            cm.Parameters.Add("@MaKhachHang", SqlDbType.Int).Value = ma;
            SqlDataReader dr = cm.ExecuteReader();
            KhachHangDTO dto = new KhachHangDTO();
            dr.Read();
            dto.MaKhachHang = dr.GetInt32(0);
            dto.TenKhachHang = dr.GetString(1);
            dto.SoDienThoai = dr.GetString(2);
            dto.DiaChi = dr.GetString(3);
            dto.Email = dr.GetString(4);
            dto.GhiChu = dr.GetString(5);
            dr.Close();
            conn.Close();
            return dto;
        }

        public string ThemKhachHang(KhachHangDTO khDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_ThemKhachHang";
                SqlParameter para;
                para = cmd.Parameters.Add("@TenKhachHang", SqlDbType.NVarChar);
                para.Value = khDTO.TenKhachHang;
                para = cmd.Parameters.Add("@SoDienThoai", SqlDbType.NVarChar);
                para.Value = khDTO.SoDienThoai;
                para = cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar);
                para.Value = khDTO.DiaChi;
                para = cmd.Parameters.Add("@Email", SqlDbType.NVarChar);
                para.Value = khDTO.Email;
                para = cmd.Parameters.Add("@GhiChu", SqlDbType.NVarChar);
                para.Value = khDTO.GhiChu;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public string CapNhatKhachHang(KhachHangDTO khDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_CapNhatKhachHang";
                SqlParameter para;
                para = cmd.Parameters.Add("@MaKhachHang", SqlDbType.Int);
                para.Value = khDTO.MaKhachHang;
                para = cmd.Parameters.Add("@TenKhachHang", SqlDbType.NVarChar);
                para.Value = khDTO.TenKhachHang;
                para = cmd.Parameters.Add("@SoDienThoai", SqlDbType.NVarChar);
                para.Value = khDTO.SoDienThoai;
                para = cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar);
                para.Value = khDTO.DiaChi;
                para = cmd.Parameters.Add("@Email", SqlDbType.NVarChar);
                para.Value = khDTO.Email;
                para = cmd.Parameters.Add("@GhiChu", SqlDbType.NVarChar);
                para.Value = khDTO.GhiChu;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public string XoaKhachHang(KhachHangDTO khDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_XoaKhachHang";
                SqlParameter para;
                para = cmd.Parameters.Add("@MaKhachHang", SqlDbType.Int);
                para.Value = khDTO.MaKhachHang;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public List<KhachHangDTO> TimKiemKhachHang(string str)
        {
            List<KhachHangDTO> lisKH = new List<KhachHangDTO>();
            SqlDataReader reader = null;
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_TimKiemKhachHang";
                cmd.Parameters.Add("@String", SqlDbType.NVarChar).Value = str;
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    KhachHangDTO khDTO = new KhachHangDTO();
                    khDTO.MaKhachHang = reader.GetInt32(0);
                    khDTO.TenKhachHang = reader.GetString(1);
                    khDTO.SoDienThoai = reader.GetString(2);
                    khDTO.DiaChi = reader.GetString(3);
                    khDTO.Email = reader.GetString(4);
                    khDTO.GhiChu = reader.GetString(5);

                    lisKH.Add(khDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return lisKH;
        }
    }
}
